/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package earthdawnnet.client;

import earthdawnnet.server.shareable.charactersheet.CharacterSheet;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;

/**
 *
 * @author Andreas Franek
 */
public class User implements Serializable
{

    private static final String fileURL = "user.cfg";
    private static User instance = null;
    private String name;
    private static Persister<User> playerPersister = new Persister<User>();
    private CharacterSheet character;

    private User()
    {
        setName("Unnamed Player");
        character = new CharacterSheet();
    }

    public String getName()
    {
        return name;
    }

    public final void setName(String name)
    {
        this.name = name;
    }

    public static User getInstance()
    {
        if (instance == null)
        {
            instance = createInstance();
        }
        return instance;
    }

    private static User createInstance()
    {
        User player = null;
        try
        {
            player = load();
        } catch (ClassNotFoundException ex)
        {
            //Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex)
        {
            //Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex)
        {
            //Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        } finally
        {
            if (player == null)
            {
                player = new User();
            }
        }
        return player;

    }

    private static User load() throws FileNotFoundException, IOException, ClassNotFoundException
    {
        return playerPersister.load(fileURL);
    }

    public static void save()
    {
        playerPersister.save(instance, fileURL);
    }

    public CharacterSheet getCharacter()
    {
        return character;
    }

    public static String getPlayerURL()
    {
        return fileURL;
    }
}
